using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.PlayerLoop;
using static UnityEditor.PlayerSettings;

public class EnemyController : MonoBehaviour
{
    [SerializeField] GameObject littleRock;
    protected Vector3 direct;
    protected PlayerControl playerControl;
    // int rockScore = 10;
    private void Start()
    {
        direct = DirectionRandom();
         playerControl =FindObjectOfType<PlayerControl>();
    }

    private void Update()
    {
        transform.Translate(direct * Time.deltaTime * 5f,Space.World);
        transform.Rotate(direct * Time.deltaTime * 10f);
    }
    Vector3 DirectionRandom()
    {
        Vector3 newPos = new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f));
        if(newPos == Vector3.zero) 
        {
            DirectionRandom();
        }
        return new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f));
    }

    void RockSplit()
    {
        int num = UnityEngine.Random.Range(1, 4);
        for(int i = 0; i < num; i++)
        {
            Instantiate(littleRock,transform.position,transform.rotation);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Bullet"))
        {
            RockSplit();
            GameManager.score += 10;
            Destroy(other.gameObject);
        }
        else if (other.CompareTag("Player"))
        {
            RockSplit();
            GameManager.score += 10;
            if(playerControl)
            {
                if(!playerControl.isShield)
                {
                    --playerControl.liveNum;
                    print(playerControl.liveNum);
                    if (playerControl.liveNum <= 0)
                    {
                        GameManager.isActivateGame = false;
                        Destroy(other.gameObject);
                    }
                }
            }
        }
        Destroy(gameObject);
    }

}
